package com.xiaozeng.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaozeng.constants.UserConstants;
import com.xiaozeng.param.UserCheckParam;
import com.xiaozeng.param.UserLoginParam;
import com.xiaozeng.pojo.User;
import com.xiaozeng.user.mapper.UserMapper;
import com.xiaozeng.user.service.UserService;
import com.xiaozeng.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * @Author: xiaozeng
 * @Date: 2022 11
 * @Description:
 **/
@Slf4j
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper,User> implements UserService {

    @Autowired
    private UserMapper userMapper;
    @Override
    public Result check(UserCheckParam userCheckParam) {
        //参数封装
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_name",userCheckParam.getUserName());
        //数据库查询
        Long total = userMapper.selectCount(queryWrapper);
        //查询结果处理
        if (total == 0){
            //数据库中不存在,可用,添加日志和返结果
            log.info("UserServiceImpl.check业务结束，结果:{}","账号可以使用!");
            return Result.ok("账号不存在,可以使用!");
        }

        log.info("UserServiceImpl.check业务结束，结果:{}","账号不可使用!");
        return Result.fail("账号已经存在,不可注册!");
    }

    @Override
    public Result register(User user) {
        //1.检查账号是否存在
        User is = this.getById(user.getUserName());

        if (is != null){
            log.info("UserServiceImpl.register业务结束，结果:{}","账号存在,注册失败!");
            return Result.fail("账号已经存在,不可注册!");
        }
        //2.密码加密处理,注意要加盐(使用Shiro)
        //String newPwd = MD5.encode(user.getPassword() + UserConstants.USER_SLAT);
        Md5Hash newPwd = new Md5Hash(user.getPassword(), UserConstants.USER_SLAT,3);
        user.setPassword(String.valueOf(newPwd));

        //3.插入数据库数据
        int rows = userMapper.insert(user);

        //4.返回封装结果
        if (rows == 0){
            log.info("UserServiceImpl.register业务结束，结果:{}","数据插入失败!注册失败!");
            return Result.fail("注册失败!请稍后再试!");
        }

        log.info("UserServiceImpl.register业务结束，结果:{}","注册成功!");
        return Result.ok("注册成功!");
    }

    @Override
    public Result login(UserLoginParam param) {
        //修改为Shiro登录
        //1.验证非空

        if (StringUtils.isEmpty(param.getUserName()) || StringUtils.isEmpty(param.getPassword())) {
            log.info("UserServiceImpl.login业务结束，结果:{}", "账号或者密码为空!");
            return Result.fail("账号或者密码为空!");
        }
        //2.验证码功能(未完成)

        //3.登录
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(param.getUserName(), param.getPassword());
        try {
            subject.login(token);
            log.info("UserServiceImpl.login业务结束，结果:{}", "登录成功!");
            return Result.ok("登录成功!", token.getUsername());
        } catch (UnknownAccountException | IncorrectCredentialsException e) {
            log.info("UserServiceImpl.login业务结束，结果:{}", "账号和密码错误!");
            return Result.fail("账号或者密码错误!");
        }
    }
        /*
        //1.密码处理
        String newPwd = MD5.encode(param.getPassword() + UserConstants.USER_SLAT);

        //2.数据库查询
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_name",param.getUserName());
        queryWrapper.eq("password",newPwd);
        User user = userMapper.selectOne(queryWrapper);

        //3.结果处理
        if (user == null) {
            log.info("UserServiceImpl.login业务结束，结果:{}","账号和密码错误!");
            return Result.fail("账号或者密码错误!");
        }
        log.info("UserServiceImpl.login业务结束，结果:{}","登录成功!");
        //不返回password属性!
        user.setPassword(null);
        return Result.ok("登录成功!",user);
    }
    */


}
